import {Component, Input} from '@angular/core';
import {NzImageModule} from "ng-zorro-antd/image";
import {MarkdownRootComponent} from "../markdown-root/markdown-root.component";
import {ChatModel, DallImage, ImageList} from "../../../user_anything/models";

@Component({
  selector: 'app-static-image',
  templateUrl: './static-image.component.html',
  styleUrl: './static-image.component.css',
  imports: [
    NzImageModule,
    MarkdownRootComponent,
  ],
  standalone: true
})
export class StaticImageComponent {
  private _chatModel: ChatModel | undefined;
  public imageList: ImageList | undefined;

  @Input()
  set chatModel(value: ChatModel | undefined) {
    this._chatModel = value;
    this.segmentsImage();
  }

  get chatModel(): ChatModel | undefined {
    return this._chatModel;
  }
  fallback =
    '';
  segmentsImage(){
    try{
      this.imageList = {
        dataId: this.chatModel?.dataId!,
        images: JSON.parse(this.chatModel?.content!) as DallImage[]
      }
    }catch (e){

    }
  }
  autoShow(image: DallImage) {
    if(!image) return "";
    if(image.url===undefined) return '';
    try{
      if(image.url==null){
        return `data:image/png;base64,${image.b64_json}`;
      }else{
        return image.url;
      }
    }catch (e){
      return ''
    }
  }
  errors() {
    return this.imageList === undefined && this.chatModel?.finish === true;
  }
}
